﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FingerVein_Database_Collection
{
    public partial class View_Captured_Data_Form : Form
    {

        System.Windows.Forms.PictureBox[] pb;
        string[] imageNo;

        public Color L_I1, L_I2, L_I3, L_I4, L_I5, L_I6;
        public Color L_M1, L_M2, L_M3, L_M4, L_M5, L_M6;
        public Color L_R1, L_R2, L_R3, L_R4, L_R5, L_R6;
        public Color R_I1, R_I2, R_I3, R_I4, R_I5, R_I6;
        public Color R_M1, R_M2, R_M3, R_M4, R_M5, R_M6;
        public Color R_R1, R_R2, R_R3, R_R4, R_R5, R_R6;


        public View_Captured_Data_Form()
        {
            InitializeComponent();
        }

        private void loadImages(Bitmap[] img)
        {
            while (flpImages.Controls.Count > 0)
            {
                var controltoremove = flpImages.Controls[0];
                flpImages.Controls.Remove(controltoremove);
                controltoremove.Dispose();
            }

            pb = new System.Windows.Forms.PictureBox[4];
            imageNo = new string[4];

            for (int i = 0; i < 4; i++)
            {
                pb[i] = new System.Windows.Forms.PictureBox();
                pb[i].Size = new Size(320, 240);
                pb[i].Image = img[i];
                flpImages.Controls.Add(pb[i]);

                imageNo[i] = i.ToString();

                Label lbl = new Label();
                lbl.Tag = i;
                lbl.Text = (i+1).ToString();
                flpImages.Controls.Add(lbl);

                pb[i].Click += new EventHandler(pb_click);
            }
        }

        private void rbLeftHand_CheckedChanged(object sender, EventArgs e)
        {
            var checkedButton = gbFingers.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton != null)
            {
                var checkedButtonHands = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
                var checkButtonFingers = gbFingers.Controls.OfType<RadioButton>()
                                        .FirstOrDefault(r => r.Checked);

                if(checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Left_Index);
                }
                else if(checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Left_Middle);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Left_Ring);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Right_Index);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Right_Middle);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Right_Ring);
                }
            }
            else
            { 
            
            }
        }

        private void rbrightHand_CheckedChanged(object sender, EventArgs e)
        {
            var checkedButton = gbFingers.Controls.OfType<RadioButton>()
                                     .FirstOrDefault(r => r.Checked);
            if (checkedButton != null)
            {
                var checkedButtonHands = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
                var checkButtonFingers = gbFingers.Controls.OfType<RadioButton>()
                                        .FirstOrDefault(r => r.Checked);

                if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Left_Index);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Left_Middle);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Left_Ring);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Right_Index);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Right_Middle);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Right_Ring);
                }
            }
            else
            {

            }
        }

        private void rbIndex_CheckedChanged(object sender, EventArgs e)
        {
            var checkedButton = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton != null)
            {
                var checkedButtonHands = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
                var checkButtonFingers = gbFingers.Controls.OfType<RadioButton>()
                                        .FirstOrDefault(r => r.Checked);

                if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Left_Index);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Left_Middle);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Left_Ring);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Right_Index);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Right_Middle);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Right_Ring);
                }
            }
            else
            {

            }
        }

        private void rbMiddle_CheckedChanged(object sender, EventArgs e)
        {
            var checkedButton = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton != null)
            {
                var checkedButtonHands = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
                var checkButtonFingers = gbFingers.Controls.OfType<RadioButton>()
                                        .FirstOrDefault(r => r.Checked);

                if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Left_Index);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Left_Middle);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Left_Ring);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Right_Index);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Right_Middle);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Right_Ring);
                }
            }
            else
            {

            }
        }

        private void rbRing_CheckedChanged(object sender, EventArgs e)
        {
            var checkedButton = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton != null)
            {
                var checkedButtonHands = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
                var checkButtonFingers = gbFingers.Controls.OfType<RadioButton>()
                                        .FirstOrDefault(r => r.Checked);

                if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Left_Index);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Left_Middle);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Left_Ring);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Right_Index);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Right_Middle);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Right_Ring);
                }
            }
            else
            {

            }
        }

        private void pb_click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure want to delete this image? ", "Message", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                var checkedButtonHands = gbhand.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
                var checkButtonFingers = gbFingers.Controls.OfType<RadioButton>()
                                        .FirstOrDefault(r => r.Checked);

                FlowLayoutPanel p = (FlowLayoutPanel)(sender as System.Windows.Forms.PictureBox).Parent;
                int Imgindex = p.Controls.GetChildIndex((sender as System.Windows.Forms.PictureBox));

                int imginx = (Imgindex / 2) ;
                
                if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Index")
                {
                    MainForm.Left_Index[imginx] = null;
                    if (imginx == 0)
                        L_I1 = Color.Red;
                    else if (imginx == 1)
                        L_I2 = Color.Red;
                    else if (imginx == 2)
                        L_I3 = Color.Red;
                    else if (imginx == 3)
                        L_I4 = Color.Red;
                    else if (imginx == 4)
                        L_I5 = Color.Red;
                    else if (imginx == 5)
                        L_I6 = Color.Red;
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Middle")
                {
                    MainForm.Left_Middle[imginx] = null;
                    if (imginx == 0)
                        L_M1 = Color.Red;
                    else if (imginx == 1)
                        L_M2 = Color.Red;
                    else if (imginx == 2)
                        L_M3 = Color.Red;
                    else if (imginx == 3)
                        L_M4 = Color.Red;
                    else if (imginx == 4)
                        L_M5 = Color.Red;
                    else if (imginx == 5)
                        L_M6 = Color.Red;
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Ring")
                {
                    MainForm.Left_Ring[imginx] = null;
                    if (imginx == 0)
                        L_R1 = Color.Red;
                    else if (imginx == 1)
                        L_R2 = Color.Red;
                    else if (imginx == 2)
                        L_R3 = Color.Red;
                    else if (imginx == 3)
                        L_R4 = Color.Red;
                    else if (imginx == 4)
                        L_R5 = Color.Red;
                    else if (imginx == 5)
                        L_R6 = Color.Red;
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Index")
                {
                    MainForm.Right_Index[imginx] = null;
                    if (imginx == 0)
                        R_I1 = Color.Red;
                    else if (imginx == 1)
                        R_I2 = Color.Red;
                    else if (imginx == 2)
                        R_I3 = Color.Red;
                    else if (imginx == 3)
                        R_I4 = Color.Red;
                    else if (imginx == 4)
                        R_I5 = Color.Red;
                    else if (imginx == 5)
                        R_I6 = Color.Red;
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Middle")
                {
                    MainForm.Right_Middle[imginx] = null;
                    if (imginx == 0)
                        R_M1 = Color.Red;
                    else if (imginx == 1)
                        R_M2 = Color.Red;
                    else if (imginx == 2)
                        R_M3 = Color.Red;
                    else if (imginx == 3)
                        R_M4 = Color.Red;
                    else if (imginx == 4)
                        R_M5 = Color.Red;
                    else if (imginx == 5)
                        R_M6 = Color.Red;
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Ring")
                {
                    MainForm.Right_Ring[imginx] = null;
                    if (imginx == 0)
                        R_R1 = Color.Red;
                    else if (imginx == 1)
                        R_R2 = Color.Red;
                    else if (imginx == 2)
                        R_R3 = Color.Red;
                    else if (imginx == 3)
                        R_R4 = Color.Red;
                    else if (imginx == 4)
                        R_R5 = Color.Red;
                    else if (imginx == 5)
                        R_R6 = Color.Red;
                }
///////////////////////////////////////////////////////////////////////////////////////////
                //refresh images
                if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Left_Index);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Left_Middle);
                }
                else if (checkedButtonHands.Text == "Left" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Left_Ring);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Index")
                {
                    loadImages(MainForm.Right_Index);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Middle")
                {
                    loadImages(MainForm.Right_Middle);
                }
                else if (checkedButtonHands.Text == "Right" && checkButtonFingers.Text == "Ring")
                {
                    loadImages(MainForm.Right_Ring);
                }
            }
            else if (dialogResult == DialogResult.No)
            {
                
            }

            


        }
    }
}
